﻿@model IEnumerable<TaskSchedule>

@{
    ViewData["Title"] = "Task Schedules";
    var totalPages = (int)Math.Ceiling((double)ViewBag.TotalCount / ViewBag.PageSize);
}

<p>
    <a asp-action="Create" class="btn btn-primary">Create New</a>
</p>

<form asp-action="Index" method="get" class="form-inline mb-3">
    <div class="form-group mr-2">
        <input type="text" name="search" class="form-control" placeholder="Search by name or URL" value="@Context.Request.Query["search"]" />
    </div>
    <input type="hidden" name="page" value="1" /> <!-- 搜索时重置页码为1 -->
    <button type="submit" class="btn btn-secondary">Search</button>
</form>

<table class="table table-striped table-bordered">
    <thead class="thead-dark">
        <tr>
            <th>Name</th>
            <th>URL</th>
            <th>Method</th>
            <th><a target="_blank" href="../../cron/cron.html">Cron Expression</a></th>
            <th>Status</th>
            <th class="table-actions">Actions</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>@Html.DisplayFor(modelItem => item.Name)</td>
                <td>@Html.DisplayFor(modelItem => item.Url)</td>
                <td>@Html.DisplayFor(modelItem => item.HttpMethod)</td>
                <td>@Html.DisplayFor(modelItem => item.CronExpression)</td>
                <td>
                    @if (item.IsActive)
                    {
                        <span class="badge rounded-pill bg-success" style="font-size: 0.9em; padding: 5px 10px;">Active</span>
                    }
                    else
                    {
                        <span class="badge rounded-pill bg-danger" style="font-size: 0.9em; padding: 5px 10px;">Inactive</span>
                    }
                </td>
                <td class="table-actions">
                    <div class="btn-group" role="group">
                        <a asp-action="Edit" asp-route-id="@item.Id" class="btn btn-sm btn-primary">Edit</a>
                        <a asp-action="Details" asp-route-id="@item.Id" class="btn btn-sm btn-info">Details</a>
                        <a asp-action="Delete" asp-route-id="@item.Id" class="btn btn-sm btn-danger">Delete</a>
                        <a asp-action="ToggleActive"
                           asp-route-id="@item.Id"
                           asp-route-search="@Context.Request.Query["search"]"
                           asp-route-page="@Context.Request.Query["page"]"
                           class="btn btn-sm @(item.IsActive ? "btn-warning" : "btn-success")">
                            @(item.IsActive ? "Deactivate" : "Activate")
                        </a>
                        @if (item.IsActive)
                        {
                            <a asp-action="TriggerNow"
                               asp-route-id="@item.Id"
                               asp-route-search="@Context.Request.Query["search"]"
                               asp-route-page="@Context.Request.Query["page"]"
                               class="btn btn-sm btn-secondary">Run Now</a>
                        }
                        <a asp-action="NextFireTimes" asp-route-id="@item.Id" target="_blank" class="btn btn-sm btn-info">Next Runs</a>
                        <a asp-action="ExecutionLogs" asp-route-id="@item.Id" target="_blank" class="btn btn-sm btn-info">Logs</a>
                    </div>
                </td>
            </tr>
        }
    </tbody>
</table>

@if (totalPages > 1)
{
    <nav>
        <ul class="pagination">
            @for (int i = 1; i <= totalPages; i++)
            {
                <li class="page-item @(i == ViewBag.Page ? "active" : "")">
                    <a class="page-link"
                       asp-action="Index"
                       asp-route-page="@i"
                       asp-route-search="@Context.Request.Query["search"]">@i</a>
                </li>
            }
        </ul>
    </nav>
}